package com.example.testcamara;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

public class RecordVideoFragment extends Fragment {
    private ImageButton btn_video;
    private ImageButton btn_torch;
    private ImageButton btn_switchCamera;
    private boolean recording = false;
    private VideoRecordCameraController mCameraControler;
    //public static String BASE_PATH;
    public static String BASE_PATH;// = Environment.getDataDirectory().getPath() + "/AAA";
    //private String TAG = "RecordVideoFragment";
    private String mFileName;
    private VideoRecordTextureView mTextureView;

    public void RecordVideoFragment(String fileName) {
        mFileName = fileName;
    }

    public View onCreateView(@NonNull LayoutInflater inflater, @NonNull
    ViewGroup container, @NonNull Bundle saveInstanceState) {
        FrameLayout layout = (FrameLayout) inflater.inflate(R.layout.fragment_record_video, container, false);
        mTextureView = new VideoRecordTextureView(layout.getContext());
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        mTextureView.setLayoutParams(params);
        layout.addView(mTextureView, 0);
        InitView(layout);
        return layout;
    }

    private void InitView(View view) {
//        mTextureView = (AutoFitTextureView) view.findViewById(R.id.tv_video);
        mCameraControler = new VideoRecordCameraController(getActivity(), mTextureView);
//        BASE_PATH = getActivity().getExternalCacheDir().getPath();
//        mCameraControler.setFolderPath(BASE_PATH);
//        mCameraControler.setFileName(mFileName);

        btn_video = view.findViewById(R.id.btn_video);
        btn_video.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (recording) {
                    recording = false;
                    mCameraControler.stopRecordingVideo();
                    btn_video.setImageResource(R.drawable.shape_red_circle);
                } else {
                    recording = true;
                    mCameraControler.startRecordingVideo();
                    btn_video.setImageResource(R.drawable.shape_red_square);
                }
            }
        });

        btn_torch = view.findViewById(R.id.btn_torch);
        btn_torch.setOnClickListener(new View.OnClickListener() {
            private boolean isFlashLightOn;

            @Override
            public void onClick(View v) {
//                mCameraHelp.changeFlash();
//                if (mCameraHelp.isFlashOpen()) {
//                    iv_flash_video.setImageResource(R.mipmap.video_flash_open);
//                } else {
//                    iv_flash_video.setImageResource(R.mipmap.video_flash_close);
//                }
//                turnFlashlightOn();
//                Toast.makeText(v.getContext(), "哈哈哈", Toast.LENGTH_SHORT).show();
//                mCameraControler.switchTorchMode(true);
                if (isFlashLightOn) {
                    mCameraControler.toggleFlashlight(false);
                } else {
                    mCameraControler.toggleFlashlight(true);
                }
                isFlashLightOn = !isFlashLightOn;
//                GeneralUtils.flashlightEvent(v.getContext(), 1);
            }
        });

        btn_switchCamera = view.findViewById(R.id.btn_switchCamera);
        btn_switchCamera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCameraControler.switchCamera();
            }
        });

        assert mTextureView != null;
        mCameraControler.initCamera();
    }

    @Override
    public void onPause() {
        super.onPause();
        mCameraControler.onPause();
    }

    public void setFileName(String fileName) {
        mFileName = fileName;
    }
}
